package org.jctc.els.modules.biz.controller.team;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.jctc.els.modules.biz.entity.team.Team;
import org.jctc.els.modules.biz.service.team.ITeamService;
import org.jctc.els.modules.biz.vo.TeamVO;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.annotation.MultipartConfig;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@RestController
@AllArgsConstructor
@RequestMapping("/team-manage")
@NonDS
@ApiIgnore
@MultipartConfig
public class TeamController extends BladeUser {

    private ITeamService teamService;

    /**
     * 分页 小组信息表
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "传入team")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teamName", value = "小组名", paramType = "query", dataType = "string")
    })
    public R<IPage<TeamVO>> list(@ApiIgnore Query query,
                                 @RequestParam(value = "teamName", required = false) String teamName,
                                 @RequestParam(value = "teamType", required = false) Integer teamType) {
        IPage<TeamVO> pages = teamService.selectAllPage(Condition.getPage(query), teamName, teamType);
        return R.data(pages);
    }

    @GetMapping("/team-leader")
    public R selectListByTeamLeader() {
        Long userId = AuthUtil.getUserId();
        List<Team> teamList = teamService.list(new QueryWrapper<Team>().eq("team_leader", userId).eq("is_deleted",0));
        return R.data(teamList);

    }

    @GetMapping("/admin")
    public R selectAdminList() {
        List<Team> teamList = teamService.list(new QueryWrapper<Team>().eq("is_deleted",0));
        return R.data(teamList);

    }


    @ApiOperation(value = "修改分组表")
    @PutMapping("/edit")
    public R edit(@RequestBody Team team) {
        team.setUpdatedTime(new Date());
        team.setUpdatedBy(AuthUtil.getUserId());
        //CacheUtil.clear(USER_CACHE);
        boolean b = teamService.update(team);
        return R.data(b);

    }

    /**
     * 新增信息表
     */
    @ApiOperation(value = "新增小组表")
    @PostMapping(value = "/add")
    public R add(@RequestBody Team team) {
        Calendar calendar = Calendar.getInstance();
        team.setCreatedTime(new Date());
        team.setUpdatedTime(new Date());
        team.setCreatedBy(AuthUtil.getUserId());
        team.setUpdatedBy(AuthUtil.getUserId());
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        team.setYear(year+"");
        return R.status(teamService.saveOrUpdate(team));
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        //TODO：删除小组的时候，需要解绑定专家，解绑项目，解绑专家参与的项目
        //TODO：删除的时候给出提示信息，让用户确认
        Boolean s = teamService.deleteTeam(ids);
        return R.status(s);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入team")
    public R<Team> detail(@RequestParam("id") Long id) {
        Team detail = teamService.selectById(id);
        return R.data(detail);
    }
}
